<template>
  <!-- 展示同一个图片及item -->
  <!-- 具名插槽 -->
  <div class="tab-bar-item"
       @click="itemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-hover"></slot>
    </div>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
      <!-- 动态获取color -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'tabbarItem',
  props: {
    path: String,
    //activeColor
    activeColor: {
      type: String,
      default: '#e4393c'
    }
  },
  data () {
    return {

    }
  },
  methods: {
    // 用计算属性来动态计算isActive是否是活跃的
    itemClick () {
      return this.$router.replace(this.path).catch(err => err)
    }
  },
  computed: {
    isActive () {
      // 设置indexOf 当不等于-1时返回true
      return this.$route.path.indexOf(this.path) !== -1

    },
    activeStyle () {
      return this.isActive ? { color: this.activeColor } : {}

    }
  }
}

</script>
<style scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  margin-top: 3px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  vertical-align: middle;
  margin: 2px 0 2px 0;
}
</style>